package com.gy.sparsearray;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 稀疏数组
 *  棋盘案例
 */
public class SparseArray {

    public static void main(String[] args) {
        //创建一个原始的二维数组11*11
        //0：表示没有棋子   1：表示黑子  2：表示白子
        int chessArr1[][] = new int[11][11];
        chessArr1[1][2]=1;
        chessArr1[1][5]=1;
        chessArr1[2][3]=2;
        chessArr1[7][3]=2;
        for (int[] row : chessArr1) {
            for (int data : row) {
                System.out.printf("%d\t",data);
            }
            System.out.println();
        }

        /**
         * 将二维数组转换成稀疏数组
         */
        //1.先遍历二维数组 得到非0的数据的个数
        int sum=0;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (chessArr1[i][j]!=0){
                    sum++;
                }
            }
        }
        System.out.println(sum);
        //2.创建对应的稀疏数组
        int sparseArr[][]=new int[sum+1][3];
        //给稀疏数组赋值
        sparseArr[0][0]=11;
        sparseArr[0][1]=11;
        sparseArr[0][2]=sum;

        //遍历二维数组，将非0的值存放到稀疏数组
        int count=0;//count 用于记录遍历到第几个非0值
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (chessArr1[i][j]!=0){
                    count++;
                    sparseArr[count][0]=i;
                    sparseArr[count][1]=j;
                    sparseArr[count][2]=chessArr1[i][j];
                }
            }
        }

        //输出稀疏数组
        System.out.println();
        for (int[] row : sparseArr) {
            for (int data : row) {
                System.out.printf("%d\t",data);
            }
            System.out.println();
        }
        File file =new File("E:\\gyWorkspace\\DataStructures\\dataStructures\\src\\main\\java\\com\\gy\\sparsearray\\sparseArr.txt");

        //if file doesnt exists, then create it
        if(!file.exists()){
            try {
                file.createNewFile();
                System.out.println("true");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        try {
            FileWriter out = new FileWriter(file);
            for (int i = 0; i < sparseArr.length; i++) {
                for (int j = 0; j < 3; j++) {
                    out.write(sparseArr[i][j]+"\t");
                    System.out.println(sparseArr[i][j]);
                }
                out.write("\n");
            }
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        List<String> list = new ArrayList<String>();
        try {
            if (file.exists() && file.isFile()){
                FileReader fileReader=new FileReader(file);
                InputStreamReader read = new InputStreamReader(
                        new FileInputStream(file));
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                while (true) {
                        if (!((lineTxt = bufferedReader.readLine()) != null)) break;
                    if (!lineTxt.startsWith("#"))
                        list.add(lineTxt);
                }
                read.close();
            }else {
                System.out.println("找不到文件");
            }
         } catch (FileNotFoundException e) {
            e.printStackTrace();
         } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(list.toString());
        String str="";
        int sparseArr2[][]=new int[list.size()][3];
        for (int i = 0; i < list.size(); i++) {
            str = list.get(i);
            String [] ss = str.split("\t");
            for(int j=0;j<3;j++)
                sparseArr2[i][j] = Integer.valueOf(ss[j]);
        }
        //输出稀疏数组
        System.out.println();
        for (int[] row : sparseArr) {
            for (int data : row) {
                System.out.printf("%d\t",data);
            }
            System.out.println();
        }

        /**
         * 将稀疏数组恢复成原始数组
         */
        int chessArr2[][]=new int[sparseArr2[0][0]][sparseArr2[0][1]];

        for (int i = 1; i < sparseArr.length; i++) {
            chessArr2[sparseArr[i][0]][sparseArr[i][1]]=sparseArr[i][2];
        }

        //输出恢复后的二维数组
        System.out.println();
        for (int[] row : chessArr2) {
            for (int data : row) {
                System.out.printf("%d\t",data);
            }
            System.out.println();
        }

    }
}
